import loadable from '@loadable/component'

import {loadQuery, } from "react-relay/hooks";
import {Environment} from "relay-runtime";
//import  TaskMainQuery  from "./__generated__/TaskMainQuery.graphql";
import {useContext} from "react";
import UserContext from "../routing/UserContext";
import * as React from "react";
import  RequestMainQuery  from "./__generated__/RequestMainQuery.graphql";

import JSResource from "../JSResource";
import {bool3t, fromObjToInput} from "../common/tool";
//路由器这里需要变量而非类型， 特殊！ 不能import {MainQuery} from "./__generated__/MainQuery.graphql";
import MainQuery from "./__generated__/MainQuery.graphql";
import StopIspLetterQuery from "./__generated__/StopIspLetterQuery.graphql";
import ModelConfigs from "../report/modelConfigs";
// import {useMainConfigQuery} from "./hook/__generated__/useMainConfigQuery.graphql";


import queryString from "query-string";

/**各类通用的申请审批执行事项的建模。?容易扩展和授权操作安全之间矛盾。
 * */
export default function secondRouter(environment: Environment) {
  return (
      [
          {
              path: '/requests',
              component: loadable(() => import('./RequestMain')),
              prepare: (params:any, user:any) => {
                  const filt= JSON.parse(sessionStorage['申请单过滤']??'{}');
                  //有些是ID携带Name的选择项，需要剔除xxxName部分。后端只需要ID，前端揭示需要名字。组件传递参数2个都要的。而ispu特殊直接ID显示也没用到Name;
                  const idfilt= fromObjToInput(filt,'dep','office','auditor','manager','director','crman');
                  console.log(`路由tasks/由params:`,params,"user=",user,"state",window.history.state?.state);
                  let vars={
                      twhere: {
                          ...idfilt,
                      }, first:5,orderBy:"mdtime",asc:false
                  };
                  console.log(`loadQuery路由前面:vars=`, vars,"myQuery=",window.history.state?.state?.myQuery);
                  //提前加载数据汇聚graphQL模式：只能在这里就要预备好各个接口函数所有的参数和数值。
                  //假如这一层次路由组件的数据需求不一样，导致分解graphql请求体文件定义，无法合并成一个graphql请求包，需多个loadQuery多次发送请求：
                  return {
                      query: loadQuery(
                          environment as any,
                          RequestMainQuery as any,
                          vars,
                          { fetchPolicy: 'store-or-network' },
                      )
                  };
              },
              routes:[
                  {
                      path: '/requests/new',   //申请框架参数选定，输入form表单关联对象ID。
                      exact: true,
                      component: loadable(() => import('./AddToRequest')),
                  },
              ],

          },
          {
              path: '/request/:rqId',
              component: loadable(() => import('./Main')),
              //【考虑】在function prepareMatches(centry, matches, user) {当中增加一个参数传递给prepare:()表示是否需要完全从后端重新加载数据，还是直接浏览器强制刷新更为直接了当？？
              //每一次流转操作之后就把申请单数据Relay.Invalidate()掉？
              prepare: (params:any) => ( {
                  //不能用'store-and-network'来替换;  默认值='store-or-network'
                  //若不加{ fetchPolicy: 'network-only' }的就算跳转进入当前页面再做RefetchQuery<>也没法给Input组件刷新初始值的已经mount没法这么刷新。
                  query: loadQuery(environment as any, MainQuery,
                      { rqId: params.rqId, },
                      { fetchPolicy: 'store-or-network' },
                  )
              } ),
          },
          {
              path: '/free/stopInspec/:rqId',
              exact: true,
              component: loadable(() => import('./StopIspLetter')),
              prepare: (params:any) => ( {
                  query: loadQuery(environment as any, StopIspLetterQuery,
                      { rqId: params.rqId, },
                      { fetchPolicy: 'store-or-network' },
                  )
              } ),
          },
      ]
  );
}

